package com.chapter13.ArrayExercise;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class ArrayExercise02 {
    public static void main(String[] args) {
        Book01[] books = new Book01[4];
        books[0] = new Book01("红楼梦", 100);
        books[1] = new Book01("金瓶梅新", 90);
        books[2] = new Book01("青年文摘20年", 5);
        books[3] = new Book01("java从入门到放弃", 300);
        Arrays.sort(books, new Comparator<Book01>() {
            @Override
            public int compare(Book01 o1, Book01 o2) {
                double a = o1.getPrice() - o2.getPrice();
                if (a > 0) {
                    return 1;
                } else if (a < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        for (int i = 0; i < books.length; i++) {
            System.out.println(books[i]);
        }

        Arrays.sort(books, new Comparator<Book01>() {
            @Override
            public int compare(Book01 o1, Book01 o2) {
                return o1.getName().length() - o2.getName().length();
            }
        });
        System.out.println("=======================================");
        for (int i = 0; i < books.length; i++) {
            System.out.println(books[i]);
        }
    }
}

class Book01 {
    private String name;
    private double price;

    public Book01(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}